package com.bs.web.rest;

import com.bs.domain.*;
import com.bs.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by wujian
 * Date: 2017-12-04
 * Time: 10:51
 */

@Api(value = "日志报警", description = "接收日志报警，邮件或短信通知相应模块负责人")
@RestController
@RequestMapping("/api/log-alert")
public class LogAlertResource {
    private final Logger log = LoggerFactory.getLogger(LogAlertResource.class);

    @Autowired
    private AlertTypeService alertTypeService;

    @Autowired
    private AlertUserService alertUserService;

    @Autowired
    private AlertSubService alertSubService;

    @Autowired
    private LogAlertService logAlertService;

    @Autowired
    private AlertMemoService alertMemoService;

    @ApiOperation(value = "查询报警类型")
    @PostMapping("/alert-type/query")
    public List<AlertType> queryAlertType(@RequestBody AlertType alertType) {
        return alertTypeService.query(alertType);
    }

    @ApiOperation(value = "保存报警类型")
    @PostMapping("/alert-type/save")
    public void saveAlertType(@RequestBody AlertType alertType) {
        alertTypeService.save(alertType);
    }

    @ApiOperation(value = "删除报警类型")
    @PostMapping("/alert-type/delete")
    public void deleteAlertType(@RequestBody AlertType alertType) {
        alertTypeService.delete(alertType);
    }

    @ApiOperation(value = "查询用户信息")
    @PostMapping("/alert-user/query")
    public List<AlertUser> queryAlertUser(@RequestBody AlertUser alertUser) {
        return alertUserService.query(alertUser);
    }

    @ApiOperation(value = "保存用户信息")
    @PostMapping("/alert-user/save")
    public void saveAlertUser(@RequestBody AlertUser alertUser) {
        alertUserService.save(alertUser);
    }

    @ApiOperation(value = "删除用户信息")
    @PostMapping("/alert-user/delete")
    public void deleteAlertUser(@RequestBody AlertUser alertUser) {
        alertUserService.delete(alertUser);
    }

    @ApiOperation(value = "查询订阅信息")
    @PostMapping("/alert-sub/query")
    public List<AlertSubQuery> queryAlertSub(@RequestBody AlertSub alertSub) {
        return alertSubService.query(alertSub);
    }

    @ApiOperation(value = "保存订阅信息")
    @PostMapping("/alert-sub/save")
    public void saveAlertSub(@RequestBody AlertSub alertSub) {
        alertSubService.save(alertSub);
    }

    @ApiOperation(value = "删除订阅信息")
    @PostMapping("/alert-sub/delete")
    public void deleteAlertSub(@RequestBody AlertSub alertSub) {
        alertSubService.delete(alertSub);
    }

    @ApiOperation(value = "查询订阅履历")
    @PostMapping("/alert-memo/query")
    public List<AlertMemoQuery> queryAlertMemo(@RequestBody AlertMemo alertMemo) {
        return alertMemoService.query(alertMemo);
    }

    @ApiOperation(value = "服务调死")
    @GetMapping("/serverdie")
    public ResponseEntity serverDieAlert(@RequestParam("time") String time, @RequestParam("partition") String partition,
                                @RequestParam("pid") String pid) {
        return logAlertService.serverDieAlert("ServerDie", time, partition, pid);
    }

    @ApiOperation(value = "发布失败")
    @GetMapping("/deployerror")
    public ResponseEntity deployErrorAlert(@RequestParam("time") String time, @RequestParam("partition") String partition,
                            @RequestParam("server_name") String serverName) {
        return logAlertService.deployErrorAlert("DeployError", time, partition, serverName);
    }

    @ApiOperation(value = "电文调用失败")
    @GetMapping("/teleerror")
    public ResponseEntity teleErrorAlert(@RequestParam("time") String time, @RequestParam("partition") String partition,
                                           @RequestParam("tc_no") String tcNo, @RequestParam("tc_msg") String tcMsg) {
        return logAlertService.teleErrorAlert("TeleError", time, partition, tcNo, tcMsg);
    }

    @ApiOperation(value = "电文调用超时")
    @GetMapping("/teletimeout")
    public ResponseEntity teleTimeoutAlert(@RequestParam("time") String time, @RequestParam("partition") String partition,
                                           @RequestParam("tc_no") String tcNo, @RequestParam("tc_time") String tcTime) {
        return logAlertService.teleTimeoutAlert("TeleTimeout", time, partition, tcNo, tcTime);
    }
    @ApiOperation(value = "日志收集异常")
    @GetMapping("/logabnormal")
    public ResponseEntity logAbnormalAlert(@RequestParam("time") String time,@RequestParam("partition") String partition,
                                           @RequestParam("es") String es){
        return logAlertService.logAbnormalAlert("LogAbnormal",time,partition,es);
    }

    @ApiOperation(value = "报警通用接口")
    @PostMapping("/common")
    public ResponseEntity commonAlert(@RequestBody AlertInfo alertInfo) {
        return logAlertService.commonAlert(alertInfo);
    }

}
